package com.vm.adapter;

import java.util.List;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.vm.activity.BitmapCache;
import com.vm.activity.GoodsDetailActivity;
import com.vm.commons.StringUtil;
import com.vm.commons.VMConst;
import com.vm.model.Goods;
import com.vm.model.Soft;
import com.vm.utils.MyDisplayImageOptions;
import com.vm.utils.SingleRequestQueue;
import com.wwt.app.R;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.view.View.OnClickListener;

/**
 * 
 * @ProjectName: [WeiMai]
 * @Author: [kent.wang]
 * @CreateDate: [2015年1月24日 上午10:01:01]
 * @Version: [v1.0]
 * 
 */

public class GoodsGridAdapter extends BaseAdapter {

	private static final String TAG = "GoodsGridAdapter";
	private Context mContext;
	private List<Goods> listGoods;
	boolean isFirstZero = true;

	/**
	 * 
	 */
	public GoodsGridAdapter(Context context, List<Goods> lists) {
		this.mContext = context;
		this.listGoods = lists;

	}

	@Override
	public int getCount() {
		return listGoods.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder viewHolder = null;
		if (convertView == null) {

			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.mall_item, null);
			viewHolder = new ViewHolder();

			/**
			 * 获取各个需要显示属性的控件
			 */
			viewHolder.iv_goodsImage = (ImageView) convertView
					.findViewById(R.id.iv_goodsImage);
			viewHolder.tv_goodsName = (TextView) convertView
					.findViewById(R.id.tv_goodsName);
//			viewHolder.tv_price = (TextView) convertView
//					.findViewById(R.id.tv_price);
			viewHolder.tv_integral = (TextView) convertView
					.findViewById(R.id.tv_integral);

			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		
		if (position == 0) {
			if (isFirstZero) {
				isFirstZero = false;
			} else {
				return convertView;
			}

		} else {
			isFirstZero = true;
		}

		final Goods goods = listGoods.get(position);

		Log.i(TAG, Integer.toString(position));

		viewHolder.tv_goodsName.setTag(goods.getId());
		viewHolder.tv_goodsName
				.setText(StringUtil.getShotName(goods.getName()));
//		viewHolder.tv_price.setText(Float.toString(goods.getPrice()));
		viewHolder.tv_integral.setText(Float.toString(goods.getIntegral()));

		ImageLoader.getInstance().displayImage(
				VMConst.URL + goods.getImageUrl(), viewHolder.iv_goodsImage,
				MyDisplayImageOptions.getInstance());
		// mImageLoader.get(VMConst.URL + goods.getImageUrl(), listener);

		convertView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(v.getContext(),
						GoodsDetailActivity.class);
				intent.putExtra("goodsId", goods.getId());
				v.getContext().startActivity(intent);
			}
		});
		return convertView;

	}

	static class ViewHolder {
		TextView tv_goodsName;
		ImageView iv_goodsImage;
		TextView tv_price;
		TextView tv_integral;
	}

}
